﻿using System;
using System.Threading.Tasks;
using System.Windows.Threading;

namespace MaxStoreErp.Core.Threading
{
    public static class ThreadHelper
    {
        static readonly Dispatcher Dispatcher = System.Windows.Application.Current.Dispatcher;

        public static void SafeCall(Action callback, bool wait = true)
        {
            if (Dispatcher.CheckAccess())
            {
                callback();
            }
            else if (wait)
            {
                Dispatcher.Invoke(callback);
            }
            else
            {
                Dispatcher.BeginInvoke(callback);
            }
        }

        public static TResult SafeCall<TResult>(Func<TResult> callback)
        {
            return Dispatcher.CheckAccess() ? callback() : Dispatcher.Invoke(callback);
        }
    }

    public static class Empty<T>
    {
        public static Task<T> Task { get { return task; } }

        private static readonly Task<T> task = System.Threading.Tasks.Task.FromResult(default(T));
    }
}
